﻿using System;
using System.Collections.Generic;
using System.Linq;
using WEB.BLL.Entities;
using WEB.BLL.Services;
using Ext.Net;
using WEB.Core.Util;
using WEB.Core.Controls;

namespace WEB.APP.Controls.System
{
    public partial class ExpertiseForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CheckPer(bool Edit)
        {
            base.CheckPer(Edit);
            txtWord.Focus(true, 50);
        }

        public void Show(decimal id)
        {
            try
            {
                var _ett = new ExpertiseEntity() { ExpertiseId = id };
                _ett = ExpertiseService.GetInstance().LoadByPrimaryKey(_ett);
                if (null == _ett)
                {
                    FormPanel1.Reset();
                    winDetails.Show();
                    return;
                }
                hfID.Text = _ett.ExpertiseId.ToString();
                txtWord.Text = _ett.Name;
                CheckPer(true);
                winDetails.Show();
            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
        public void Show()
        {
            FormPanel1.Reset();
            winDetails.Show();
            CheckPer(false);
        }

        protected void btnNew_Click(object sender, DirectEventArgs e)
        {
            FormPanel1.Reset();
            CheckPer(false);
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var _ett = new ExpertiseEntity() { Name = txtWord.Text.Trim() };
                if (!string.IsNullOrEmpty(hfID.Text))
                {
                    _ett.ExpertiseId = Convert.ToDecimal(hfID.Text);
                    ExpertiseService.GetInstance().Update(_ett);
                }
                else
                {
                    ExpertiseService.GetInstance().Insert(_ett);
                    hfID.Text = _ett.ExpertiseId.ToString();
                }
                CheckPer(true);
                //winDetails.Hide();
                OnSaveClick();

            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}